package com.spring.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.spring.annotation.RequiredLog;
import com.spring.annotation.RequiredTime;
import com.spring.pojo.Article;
import com.spring.pojo.vo.JsonResult;
import com.spring.service.ArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "文章管理")
@RestController
public class ArticleController {

    //@Resource(name = "articleServiceImpl")
    @Autowired
    @Qualifier("articleServiceImpl")
    private ArticleService articleService;

    /**
     * @RequiredLog 注解描述的方法为一个日志切入点方法
     * @param articleId
     * @return
     */
    @ApiImplicitParam(name="articleId",value = "文章id",
            dataType = "long",required = true,example = "1")
    @ApiOperationSupport(order = 20)
    @ApiOperation("查看文章")
    @RequiredLog(operation = "浏览文章")
    @GetMapping("/article/{articleId}")
    public JsonResult doSelectById(@PathVariable("articleId") Long articleId){
        if(articleId<0)
            throw new IllegalArgumentException("文章id不合法");
        Article article=articleService.selectById(articleId);
        return new JsonResult(article);
    }

    @RequiredTime
    @ApiOperationSupport(order = 10)
    @ApiOperation("文章列表")
    @GetMapping("/article/list")
    public JsonResult doList(){
        System.out.println("获取文章列表");
        List<Article> list = articleService.list();
        return new JsonResult(list);
    }

    @RequiredTime
    @RequiredLog(operation = "发布文章")
    @ApiOperationSupport(order = 30)
    @ApiOperation("发布文章")
    @PostMapping("/article/create")
    public JsonResult doCreate(@RequestBody Article article){
        System.out.println("article="+article);
        articleService.insert(article);
        return new JsonResult("create ok");
    }



}

